<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信息服务管理</title>

    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">

    <script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
    
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
    
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">丢失的物品</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="lost_thing" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                          
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
    
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                    
                </div>
            </script>
    
            <table class="layui-hide" id="lostTable" lay-filter="currentTableFilter"></table>
    
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>
    
        </div>
    </div>

    <script id="picTpl">
        <img src="{{d.lost_pic}}" name="lost_pic" style="width:100%; height:100%;"/>
    </script>

    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
    
            table.render({
                elem: '#lostTable',
                url: 'http://localhost:8080/lost/lostList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {type: "checkbox", width: 100},
                    {field: 'lost_id', width: 80, title: 'ID', sort: true},
                    {field: 'lost_thing', width: 200, title: '找到的东西'},
                    {field: 'lost_phone', title: '失主电话', minWidth: 150},
                    {field: 'lost_name', title: '失主姓名', minWidth: 150},
                    {field: 'lost_pic', width: 150, title: '图片' ,templet: '#picTpl'},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            });
    
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });
    
                //执行搜索重载
                table.reload('lostTable', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        // searchParams: result
                        lost_thing: data.field.lost_thing,
                        
                    }
                }, 'data');
    
                return false;
            });
    
            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加信息',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: 'lostAdd.html',
                        end:function(index){
                            table.reload('lostTable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('lostTable')
                        , data = checkStatus.data;
                        console.log("data" + data)
                    //1-2-3-4-
                    var str = "";
                    for (var i in data) {
                        str += data[i].lost_id + "-";
                    }
                    var data2 = { "lost_id": str };
                    //获取选中数量
                    var selectCount = checkStatus.data.length;
                    if (selectCount == 0) {
                        layer.msg('至少选一个吧', function () { });
                        return false;
                    }
                    layer.confirm('真的要删除选中的项吗？', function (index) {
                        layer.close(index);
                    })
                    $.ajax({
                        type: "get",
                        data: data2,
                        url: "http://localhost:8080/lost/deleteLostAll",
                        success: function (data) {
                            if (data.code == 0) {
                                $('#reload').click();
                            }
                            layer.close(index);
                            layer.msg('删除成功！');
                            table.reload('lostTable', {});
                        }, erro: function (code) {
                            layer.msg('操作失败！');
                        }
                    })
                }
                

            });
    
            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });
    
            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    
                   //把要修改的数据存到 localStorage        跳转到修改页面之后  再取出来 
                    localStorage.setItem("find",JSON.stringify(data));
                    var index = layer.open({
                        title: '编辑信息',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: 'lostEdit.html',
                        end:function(index){
                            // 列表重新加载
                            table.reload('lostTable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        $.get("http://localhost:8080/lost/deleteLost",{lost_id:data.lost_id},function(result){
                            if(result.code==0){
                                layer.msg("删除成功");
                                obj.del();   //删除对应的  tr  删除页面上的dom 结构

                            }    
                            
                        },"json");    

                        layer.close(index);
                    });
                }
            });
    
        });
    </script>
</body>
</html>